home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / SideScroller / Ground.as < prev    next >
Encoding:
Text File  |  2007-09-27  |  5.8 KB  |  185 lines

  1. class SideScroller.Ground
  2. {
  3.    var mcRef;
  4.    var oParentObject;
  5.    var nMinX;
  6.    var nMaxX;
  7.    var nMinY;
  8.    var nMaxY;
  9.    var aPoints;
  10.    var nSearchProgression;
  11.    static var aGrounds;
  12.    static var aActiveWorkGrounds;
  13.    static var oEnterFrameListener;
  14.    static var bInited;
  15.    static var ITERATION_X = 25;
  16.    static var ITERATION_Y = 10;
  17.    static var SEARCHS_PER_FRAME = 1;
  18.    static var MAX_WORKING_GROUNDS = 3;
  19.    function Ground(__mcRef, __oParentObject)
  20.    {
  21.       this.mcRef = __mcRef;
  22.       this.oParentObject = __oParentObject;
  23.       this.mcRef._visible = false;
  24.       var _loc2_ = this.mcRef.getBounds(this.oParentObject.ParentLayer.Ref);
  25.       this.nMinX = _loc2_.xMin;
  26.       this.nMaxX = _loc2_.xMax;
  27.       this.nMinY = _loc2_.yMin;
  28.       this.nMaxY = _loc2_.yMax;
  29.       this.aPoints = new Array();
  30.       this.nSearchProgression = this.nMinX + (SideScroller.Ground.ITERATION_X - this.nMinX % SideScroller.Ground.ITERATION_X);
  31.       SideScroller.Ground.doScheduleGround(this);
  32.    }
  33.    static function doStaticEnterFrame()
  34.    {
  35.       if(SideScroller.Ground.aGrounds.length > 0)
  36.       {
  37.          if(SideScroller.Ground.aActiveWorkGrounds.length < SideScroller.Ground.MAX_WORKING_GROUNDS)
  38.          {
  39.             SideScroller.Ground.doStartGroundWork();
  40.          }
  41.       }
  42.       for(var _loc1_ in SideScroller.Ground.aActiveWorkGrounds)
  43.       {
  44.          SideScroller.Ground.aActiveWorkGrounds[_loc1_].doEnterFrame();
  45.       }
  46.    }
  47.    static function doClean()
  48.    {
  49.       delete SideScroller.Ground.aGrounds;
  50.       delete SideScroller.Ground.aActiveWorkGrounds;
  51.       MovieClip.removeListener(SideScroller.Ground.oEnterFrameListener);
  52.       delete SideScroller.Ground.oEnterFrameListener.onEnterFrame;
  53.       delete SideScroller.Ground.oEnterFrameListener;
  54.       SideScroller.Ground.bInited = false;
  55.    }
  56.    static function doInit()
  57.    {
  58.       mx.transitions.OnEnterFrameBeacon.init();
  59.       SideScroller.Ground.oEnterFrameListener = new Object();
  60.       SideScroller.Ground.oEnterFrameListener.onEnterFrame = Library.Utils.Delegate.create(SideScroller.Ground,SideScroller.Ground.doStaticEnterFrame);
  61.       MovieClip.addListener(SideScroller.Ground.oEnterFrameListener);
  62.       SideScroller.Ground.aGrounds = new Array();
  63.       SideScroller.Ground.aActiveWorkGrounds = new Array();
  64.       SideScroller.Ground.bInited = true;
  65.    }
  66.    static function doStartGroundWork()
  67.    {
  68.       var _loc4_ = undefined;
  69.       var _loc1_ = Infinity;
  70.       var _loc2_ = undefined;
  71.       for(var _loc3_ in SideScroller.Ground.aGrounds)
  72.       {
  73.          if(SideScroller.Ground.aGrounds[_loc3_].StartPos < _loc1_)
  74.          {
  75.             _loc1_ = SideScroller.Ground.aGrounds[_loc3_].StartPos;
  76.             _loc2_ = Number(_loc3_);
  77.          }
  78.       }
  79.       _loc4_ = SideScroller.Ground.aGrounds[_loc2_];
  80.       SideScroller.Ground.aGrounds.splice(_loc2_,1);
  81.       SideScroller.Ground.aActiveWorkGrounds.push(_loc4_);
  82.    }
  83.    static function onGroundWorkComplete(__oGround)
  84.    {
  85.       for(var _loc2_ in SideScroller.Ground.aActiveWorkGrounds)
  86.       {
  87.          if(SideScroller.Ground.aActiveWorkGrounds[_loc2_] == __oGround)
  88.          {
  89.             SideScroller.Ground.aActiveWorkGrounds.splice(Number(_loc2_),1);
  90.          }
  91.       }
  92.    }
  93.    static function doScheduleGround(__oGround)
  94.    {
  95.       if(!SideScroller.Ground.bInited)
  96.       {
  97.          SideScroller.Ground.doInit();
  98.       }
  99.       SideScroller.Ground.aGrounds.push(__oGround);
  100.    }
  101.    function doEnterFrame()
  102.    {
  103.       var _loc7_ = 1;
  104.       while(_loc7_ <= SideScroller.Ground.SEARCHS_PER_FRAME)
  105.       {
  106.          if(this.nSearchProgression <= this.nMaxX)
  107.          {
  108.             var _loc6_ = this.nMaxY;
  109.             var _loc2_ = undefined;
  110.             _loc2_ = this.nMaxY - 1;
  111.             while(_loc2_ >= this.nMinY)
  112.             {
  113.                var _loc4_ = this.nSearchProgression + this.oParentObject.ParentLayer.Ref._x;
  114.                var _loc3_ = _loc2_ + this.oParentObject.ParentLayer.Ref._y;
  115.                var _loc5_ = this.mcRef.hitTest(_loc4_,_loc3_,true);
  116.                if(_loc5_)
  117.                {
  118.                   _loc6_ = _loc2_;
  119.                }
  120.                else
  121.                {
  122.                   _loc2_ = -Infinity;
  123.                }
  124.                _loc2_ -= SideScroller.Ground.ITERATION_Y;
  125.             }
  126.             this.aPoints.push({nX:this.nSearchProgression,nY:_loc6_});
  127.             this.nSearchProgression += SideScroller.Ground.ITERATION_X;
  128.          }
  129.          else
  130.          {
  131.             SideScroller.Ground.onGroundWorkComplete(this);
  132.          }
  133.          _loc7_ = _loc7_ + 1;
  134.       }
  135.    }
  136.    function getGroundAt(__nPositionX)
  137.    {
  138.       var _loc3_ = __nPositionX - __nPositionX % SideScroller.Ground.ITERATION_X;
  139.       var _loc6_ = _loc3_ + SideScroller.Ground.ITERATION_X;
  140.       var _loc4_ = this.nMaxY;
  141.       var _loc5_ = this.nMaxY;
  142.       var _loc2_ = undefined;
  143.       _loc2_ = 0;
  144.       while(_loc2_ <= this.aPoints.length - 1)
  145.       {
  146.          if(this.aPoints[_loc2_].nX == _loc3_)
  147.          {
  148.             _loc4_ = this.aPoints[_loc2_].nY;
  149.          }
  150.          if(this.aPoints[_loc2_].nX == _loc6_)
  151.          {
  152.             _loc5_ = this.aPoints[_loc2_].nY;
  153.          }
  154.          _loc2_ = _loc2_ + 1;
  155.       }
  156.       var _loc8_ = (__nPositionX - _loc3_) / (_loc6_ - _loc3_);
  157.       var _loc10_ = _loc5_ - _loc4_;
  158.       var _loc9_ = _loc4_ + _loc10_ * _loc8_;
  159.       return _loc9_;
  160.    }
  161.    function doDestroy()
  162.    {
  163.       this.oParentObject.ParentLayer.doRemoveListener(this);
  164.       this.aPoints = new Array();
  165.       delete this.aPoints;
  166.       delete this.oParentObject;
  167.    }
  168.    function get StartPos()
  169.    {
  170.       return this.nMinX;
  171.    }
  172.    function get MinY()
  173.    {
  174.       return this.nMinY;
  175.    }
  176.    function get MaxY()
  177.    {
  178.       return this.nMaxY;
  179.    }
  180.    function get EndPos()
  181.    {
  182.       return this.nMaxX;
  183.    }
  184. }
  185.